/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.MessageImpl;
import com.lcfgly.Database.Message;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.SQLEmptyException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息操作
 * @author lcfgly
 */
@Service
public class MessageService implements Serializable{
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    MessageImpl messageImpl;
    
    @Autowired
    UserService userService;
    
    /**
     * 保存消息
     * @param arg0
     * @throws ActionException 
     */
    public void save(Message arg0)throws ActionException{
        if(arg0 ==null || arg0.getGoid()== 0 || arg0.getId() == 0)
            throw new ActionException("更新数据不完整");
        try{
            messageImpl.update(arg0);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 保存数据
     * @param arg0
     * @throws ActionException 
     */
    public void create(Message arg0) throws ActionException{ 
        if(arg0 == null || arg0.getGoid()== 0)
            throw new ActionException("数据不完整");
        try{
            if(messageImpl.insert(arg0) != 1)
                throw new ActionException("数据保存失败！");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取已回复的数据列表
     * @param shopid
     * @param page
     * @return
     * @throws ActionException 
     */
    public Map GetBackMessage(int shopid,int page) throws ActionException{
        try{
            return messageImpl.GetBackMessage(shopid, page);
        }catch(SQLEmptyException e){
            log.info(e);
            return null;
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取未回复的数据列表
     * @param shopid
     * @param page
     * @return
     * @throws ActionException 
     */
    public Map GetNotBackMessage(int shopid,int page)throws ActionException{
        try{
            return messageImpl.GetNotBackMessage(shopid, page);
        }catch(SQLEmptyException e){
            log.info(e);
            return null;
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 删除数据
     * @param id
     * @param shopid
     * @throws ActionException 
     */
    public void delete(int id,int shopid)throws ActionException{
        try{
            messageImpl.delete(id, shopid);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 按照ID查找数据
     * @param id
     * @param to
     * @return
     * @throws ActionException 
     */
    public Message findbyId(int id,int to)throws ActionException{
        try{
           return messageImpl.findById(id, to);
        }catch(SQLEmptyException e){
            log.info(e);
            throw new ActionException("未找到先关数据!");
        }catch(SQLException  e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取首页的评论列表
     * @param shopid
     * @return 
     */
    public List indexMessage(int shopid){
        try{
            Map res=messageImpl.IndexGetMessage(shopid);
            if (res.get("data") != null) {
                List resdata=(List)res.get("data");
                List result=new ArrayList();
                for (Object object : resdata) {
                    Map r = new HashMap();
                    Message m = (Message) object;
                    try {
                        r.put("user", userService.findUsersById(m.getComeid()));
                    } catch (SQLException e) {
                        HashMap user = new HashMap();
                        user.put("uname", "网友");
                        r.put("user", user);
                        log.error(e);
                    }
                    r.put("message", m);
                    result.add(r);
                }
                return result;
            }
            return null;
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
    /**
     * 获取评论数据
     * @param shopid
     * @param page
     * @return
     * @throws ActionException 
     */
    public Map MessageList(int shopid,int page)throws ActionException{
        try{
            Map res=messageImpl.GetMessage(shopid, page);
            if (res.get("data") != null) {
                List resdata=(List)res.get("data");
                List result=new ArrayList();
                for (Object object : resdata) {
                    Map r = new HashMap();
                    Message m = (Message) object;
                    try {
                        r.put("user", userService.findUsersById(m.getComeid()));
                    } catch (SQLException e) {
                        HashMap user = new HashMap();
                        user.put("uname", "网友");
                        r.put("user", user);
                        log.error(e);
                    }
                    r.put("message", m);
                    result.add(r);
                }
                res.put("data", result);
            }
            return res;
        }catch(SQLEmptyException e){
            log.info(e);
            return null;
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
}
